//
//  LXUserAccountTools.swift
//  weibo
//
//  Created by 罗心 on 16/6/28.
//  Copyright © 2016年 罗心. All rights reserved.
//

import UIKit

class LXUserAccountTools: NSObject {
    let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.archiver")
    // 保存的用户信息
    var userAccount: LXUserAccount?
    
    // 判断用户是否登录
    var isLogin:Bool {
        return accessToken != nil
    }
    
    var accessToken: String? {
        if userAccount?.access_token == nil {
            return nil
        }else {
            if userAccount?.expires_Date?.compare(NSDate()) == NSComparisonResult.OrderedDescending{
                return userAccount?.access_token
            }else {
                return nil
            }
        }
    }
    

    // 全局访问点
    static let sharedTools:LXUserAccountTools = LXUserAccountTools()
    
    override init() {
        super.init()
        userAccount = getUserAccount()
    }
    // 保存对象
    func saveUserAccount(userAccount: LXUserAccount){

        self.userAccount = userAccount
        
        NSKeyedArchiver.archiveRootObject(userAccount, toFile: path)
    }
    // 获取对象
    func getUserAccount() ->LXUserAccount?{

        let result = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? LXUserAccount
        return result
    }

}
